﻿using DX_Recognize.Common;
using DX_Recognize.Entitys;
using DX_Recognize.Utils;
using DX_Recognize.View;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.Internal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using AcadApp = ZwSoft.ZwCAD.ApplicationServices.Application;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.GraphicsInterface;
using DX_Recognize.PageView;
using ZwSoft.ZwCAD.Geometry;
using ZwSoft.ZwCAD.EditorInput;
using DX_Recognize.AuxiliaryTool;
namespace DX_Recognize
{
    public partial class FrameWindow : Form
    {
        // Reference origin coordinates
        CAD_Point RefOPoint = new CAD_Point(0, 0, 0);
        double GsViewW = 0;
        double GsViewL = 0;
        string frameLayerName = "DXFrameLayer";
        ColRuleWindow colRuleWindow = new ColRuleWindow();
        //板编号
        String BoardCode = null;
        int operIndex = -1;
        //自动 手动 共用
        public FrameItem originOperFrameInfo = null;
        public FrameItem operFItem = null;
        public BoardSectionItem APageOperBItem = null;
        ManualBoardItem MPageOperBItem = null;
        List<StandardHoleData> GPList = new List<StandardHoleData>();
        List<StandardHoleData> SPList = new List<StandardHoleData>();
        List<StandardHoleData> RPList = new List<StandardHoleData>();
        FramePhaseCol framePhaseCol = new FramePhaseCol();
        //提示语
        List<string> colErrorList = new List<string>();
        //是否有相崩报错提示
        Boolean HasColErrorF = false;
        public FrameWindow()
        {
            InitializeComponent();
            PitchColErrorTigLablel.AutoSize = true;
            Database initDb = new Database();
            InitDrawingControl(AcadApp.DocumentManager.MdiActiveDocument, initDb);
            this.Hide();
            
        }

        public event Action<int, string> FrameSideMeaAction;
        public event Action<int> MTypeRenderAboutFrameUIAction;
        public event Action MotifyToChangeMainWinHoleUIAction;

        //重置状态 处理相崩提示
        public void ResetColErrorTig()
        {
            HasColErrorF = false;
            PitchColErrorTigLablel.Text = "暂无";
        }

        //从主界面调过来
        public void InitFrameWindow(int rowIndex)
        {
            operIndex = rowIndex;
            ResetColErrorTig();
            if (AssemblyShareData.operType == CustomOperPatternType.AUTO)
            {
                List<BoardSectionItem> targetList = AssemblyShareData.AUIBlueSectionList;
                this.BoardFrameBox.DataSource = targetList;
                BoardFrameBox.DisplayMember = "BoardName";
                BoardFrameBox.ValueMember = "BoardUID";
                SetAutoPageFrameLogicAndUIData(rowIndex);
            }
            if (AssemblyShareData.operType == CustomOperPatternType.MANUAL)
            {
                List<ManualBoardItem> targetList = AssemblyShareData.MSectionList;
                this.BoardFrameBox.DataSource = targetList;
                BoardFrameBox.DisplayMember = "BoardName";
                BoardFrameBox.ValueMember = "BoardUID";
                SetMPageFrameLogicAndUIData(rowIndex);
            }
            HoleBtnShowAble();
            this.NormalShow();
        }

        //About how to use DWG PreView Control
        public void InitDrawingControl(Document doc, Database db)
        {
            this.mPreviewCtrl.ClearAll();
            mPreviewCtrl.Init(doc, db);
            SetViewTo(mPreviewCtrl.mpView, db);
            using (BlockTableRecord curSpace = db.CurrentSpaceId.Open(OpenMode.ForRead, true, true) as BlockTableRecord)
                mPreviewCtrl.mpView.Add(curSpace, mPreviewCtrl.mpModel);
            ChangeViewStyleTo(VisualStyleType.Basic);
            ChangeRenderModeTo(ZwSoft.ZwCAD.GraphicsSystem.RenderMode.Wireframe);
        }

        public void SetViewTo(ZwSoft.ZwCAD.GraphicsSystem.View view, Database db)
        {
            if (db.Extmax.X < db.Extmin.X || db.Extmax.Y < db.Extmin.Y || db.Extmax.Z < db.Extmax.Z)
            {
                db.Extmin = new Point3d(0, 0, 0);
                db.Extmax = new Point3d(400, 400, 400);
            }
            Point3d extMax = db.Extmax;
            Point3d extMin = db.Extmin;
            // now the active viewport info
            double height = 0.0, width = 0.0, viewTwist = 0.0;
            Point3d targetView = new Point3d();
            Vector3d viewDir = new Vector3d();
            GSUtil.GetActiveViewPortInfo(ref height, ref width, ref targetView, ref viewDir, ref viewTwist, true);
            viewDir = viewDir.GetNormal();
            Vector3d viewXDir = viewDir.GetPerpendicularVector().GetNormal();
            viewXDir = viewXDir.RotateBy(viewTwist, -viewDir);
            Vector3d viewYDir = viewDir.CrossProduct(viewXDir);
            Point3d boxCenter = new Point3d(RefOPoint.X, RefOPoint.Y,0);
            Matrix3d viewMat;
            viewMat = Matrix3d.AlignCoordinateSystem(boxCenter, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
              boxCenter, viewXDir, viewYDir, viewDir).Inverse();
            Extents3d wcsExtents = new Extents3d(extMin, extMax);
            Extents3d viewExtents = wcsExtents;
            viewExtents.TransformBy(viewMat);
            double xMax = 0;
            double yMax = 0;
            if (GsViewW == 0)
                xMax = 1000;
            if (GsViewL == 0)
                yMax = 1000;
            Point3d eye = boxCenter + viewDir;
            view.SetView(eye, boxCenter, viewYDir, xMax, yMax);
            refreshView();
        }

        public void ChangeViewStyleTo(VisualStyleType vs)
        {
            refreshView();
        }

        public void refreshView()
        {
            mPreviewCtrl.mpView.Invalidate();
            mPreviewCtrl.mpView.Update();
        }

        public void ChangeRenderModeTo(ZwSoft.ZwCAD.GraphicsSystem.RenderMode rm)
        {
            mPreviewCtrl.mpView.Mode = rm;
            refreshView();
        }
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            // if we are wheeling down
            if (e.Delta < 0)
                mPreviewCtrl.mpView.Zoom(0.5);
            else
                // wheel up!
                mPreviewCtrl.mpView.Zoom(1.5);

            refreshView();
        }

        private void NormalShow()
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            this.Show();
        }

        //保存孔距 UI组件 显示隐藏
        public void HoleBtnShowAble()
        {
            if (AssemblyShareData.operType == CustomOperPatternType.AUTO)
                ComfirmSaveHoleBtn.Visible = false;
            if (AssemblyShareData.operType == CustomOperPatternType.MANUAL)
                ComfirmSaveHoleBtn.Visible = true;
        }

        //手动模式
        public void SetMPageFrameLogicAndUIData(int Index)
        {
            List<ManualBoardItem> targetList = AssemblyShareData.MSectionList;
            BoardFrameBox.SelectedIndex = Index;
            MPageOperBItem = targetList[Index];
            BoardCode = MPageOperBItem.BoardCode;
            operFItem = MPageOperBItem.frameItem;
            originOperFrameInfo = (FrameItem)ConvertUtils.DeepCopyObject(operFItem);
            SetFrameBaseUIData();
            MPageSetHoleUIData();
        }

        //自动模式
        public void SetAutoPageFrameLogicAndUIData(int Index)
        {
            List<BoardSectionItem> targetList = AssemblyShareData.AUIBlueSectionList;
            BoardFrameBox.SelectedIndex = Index;
            APageOperBItem = targetList[Index];
            BoardCode = APageOperBItem.BoardCode;
            operFItem = APageOperBItem.frameItem;
            originOperFrameInfo = (FrameItem)ConvertUtils.DeepCopyObject(operFItem);
            SetFrameBaseUIData();
            AutoPageSetHoleUIData();
        }

        //设置界面数据
        private void SetFrameBaseUIData()
        {
            this.frameLBox.Value = (decimal)operFItem.FrameLength;
            this.frameWBox.Value = (decimal)operFItem.FrameWidth;
            this.frameDBox.Value = (decimal)operFItem.FrameDepth;

            this.TLValue.Value = (decimal)operFItem.TL_Radius;
            this.TRValue.Value = (decimal)operFItem.TR_Radius;
            this.BLValue.Value = (decimal)operFItem.BL_Radius;
            this.BRValue.Value = (decimal)operFItem.BR_Radius;

            if (operFItem.TL_Type == AngleType.COR)
                this.TLType.SelectedItem = "R";
            if (operFItem.TL_Type == AngleType.BK)
                this.TLType.SelectedItem = "避空";
            if (operFItem.TL_Type == AngleType.XBK)
                this.TLType.SelectedItem = "X避空";
            if (operFItem.TL_Type == AngleType.YBK)
                this.TLType.SelectedItem = "Y避空";

            if (operFItem.TR_Type == AngleType.COR)
                this.TRType.SelectedItem = "R";
            if (operFItem.TR_Type == AngleType.BK)
                this.TRType.SelectedItem = "避空";
            if (operFItem.TR_Type == AngleType.XBK)
                this.TRType.SelectedItem = "X避空";
            if (operFItem.TR_Type == AngleType.YBK)
                this.TRType.SelectedItem = "Y避空";

            if (operFItem.BL_Type == AngleType.COR)
                this.BLType.SelectedItem = "R";
            if (operFItem.BL_Type == AngleType.BK)
                this.BLType.SelectedItem = "避空";
            if (operFItem.BL_Type == AngleType.XBK)
                this.BLType.SelectedItem = "X避空";
            if (operFItem.BL_Type == AngleType.YBK)
                this.BLType.SelectedItem = "Y避空";

            if (operFItem.BR_Type == AngleType.COR)
                this.BRType.SelectedItem = "R";
            if (operFItem.BR_Type == AngleType.BK)
                this.BRType.SelectedItem = "避空";
            if (operFItem.BR_Type == AngleType.XBK)
                this.BRType.SelectedItem = "X避空";
            if (operFItem.BR_Type == AngleType.YBK)
                this.BRType.SelectedItem = "Y避空";

            Boolean F1 = (operFItem.BL_Type == operFItem.TL_Type) && (operFItem.BL_Type == operFItem.TR_Type) 
                && (operFItem.BL_Type == operFItem.BR_Type);
            this.cAllType.SelectedIndex = this.BLType.SelectedIndex;

            Boolean F2 = (this.BLValue.Value == this.TLValue.Value) && (this.BLValue.Value == this.TRValue.Value)
                && (this.BLValue.Value == this.BRValue.Value);
            if(F2)
                this.cAllValue.Value = this.BLValue.Value;

        }

        //手动模式 孔距UI
        public void MPageSetHoleUIData()
        {
            List<ManualItem> tempList = ShareItemData.MItemList;
            if (tempList.Count > 0)
            {
                for (int i = 0; i < tempList.Count; i++)
                {
                    string matchCode = tempList[i].ItemCode;
                    if (matchCode.Equals("GuidePin"))
                    {
                        this.GPDim.Value = (decimal)tempList[i].Dim;
                        this.GPSpacingW.Value = (decimal)tempList[i].ItemHW;
                        this.GPSpacingL.Value = (decimal)tempList[i].ItemHL;
                    }
                    if (matchCode.Equals("SupportPin") || matchCode.Equals("NoNozzleMesonSupportPin"))
                    {
                        this.SPDim.Value = (decimal)tempList[i].Dim;
                        this.SPSpacingW.Value = (decimal)tempList[i].ItemHW;
                        this.SPSpacingL.Value = (decimal)tempList[i].ItemHL;
                    }
                    if (matchCode.Equals("ReturnPin"))
                    {
                        this.RPDim.Value = (decimal)tempList[i].Dim;
                        this.RPSpacingW.Value = (decimal)tempList[i].ItemHW;
                        this.RPSpacingL.Value = (decimal)tempList[i].ItemHL;
                    }
                    if (matchCode.Equals("BottomPanelScrew"))
                    {
                        this.BPDim.Value = (decimal)tempList[i].Dim;
                        this.BPSpacingW.Value = (decimal)tempList[i].ItemHW;
                        this.BPSpacingL.Value = (decimal)tempList[i].ItemHL;
                    }
                    if (matchCode.Equals("PinBoardScrew"))
                    {
                        this.EPFPDim.Value = (decimal)tempList[i].Dim;
                        this.EPFPSpacingW.Value = (decimal)tempList[i].ItemHW;
                        this.EPFPSpacingL.Value = (decimal)tempList[i].ItemHL;
                    }
                    if (matchCode.Equals("TopPanelScrew"))
                    {
                        this.TPDim.Value = (decimal)tempList[i].Dim;
                        this.TPSpacingW.Value = (decimal)tempList[i].ItemHW;
                        this.TPSpacingL.Value = (decimal)tempList[i].ItemHL;
                    }
                    if (matchCode.Equals("SpaceBlockScrew"))
                    {
                        this.fangtieDim.Value = (decimal)tempList[i].Dim;
                        this.fangtieSpacingW.Value = (decimal)tempList[i].ItemHW;
                        this.fangtieSpacingL.Value = (decimal)tempList[i].ItemHL;
                    }
                    if (matchCode.Equals("EGPPin"))
                    {
                        this.EGPDim.Value = (decimal)tempList[i].Dim;
                        this.EGPSpacingW.Value = (decimal)tempList[i].ItemHW;
                        this.EGPSpacingL.Value = (decimal)tempList[i].ItemHL;
                    }
                }
            }
        }

        public void ATypeClearHoleUI()
        {
            this.GPSpacingW.Value = 0;
            this.GPSpacingL.Value = 0;
            this.GPDim.Value = 0;

            this.RPSpacingW.Value = 0;
            this.RPSpacingL.Value = 0;
            this.RPDim.Value = 0;

            this.SPSpacingW.Value = 0;
            this.SPSpacingL.Value = 0;
            this.SPDim.Value = 0;

            this.BPSpacingW.Value = 0;
            this.BPSpacingL.Value = 0;
            this.BPDim.Value = 0;

            this.TPSpacingW.Value = 0;
            this.TPSpacingL.Value = 0;
            this.TPDim.Value = 0;

            this.EPFPSpacingW.Value = 0;
            this.EPFPSpacingL.Value = 0;
            this.EPFPDim.Value = 0;

            this.fangtieSpacingW.Value = 0;
            this.fangtieSpacingL.Value = 0;
            this.fangtieDim.Value = 0;

            this.EGPSpacingW.Value = 0;
            this.EGPSpacingL.Value = 0;
            this.EGPDim.Value = 0;
        }
        //自动模式 孔距UI
        public void AutoPageSetHoleUIData()
        {
            ATypeClearHoleUI();
            //导柱
            int gpIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
            DItemNameType.GuidePin);
            if (gpIndex != -1)
            {
                SingleItem GPItem = ShareItemData.ItemList[gpIndex];
                this.GPDim.Value = Convert.ToDecimal(GPItem.Dim);
                List<StandardHoleData> GPHolePList = GPItem.HSplitInfo.SplitInfoList;
                if (GPHolePList.Count > 0)
                {
                    int FIndex = GPHolePList.FindIndex(item => !item.partialF);
                    if (FIndex != -1)
                    {
                        StandardHoleData targetHoleItem = GPHolePList[FIndex];
                        this.GPSpacingW.Value = (decimal)Math.Abs(targetHoleItem.ItemX) * 2;
                        this.GPSpacingL.Value = (decimal)Math.Abs(targetHoleItem.ItemY) * 2;
                    }
                }
            }

            //回针
            int rpIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
            DItemNameType.ReturnPin);
            if (rpIndex != -1)
            {
                SingleItem RPItem = ShareItemData.ItemList[rpIndex];
                this.RPDim.Value = Convert.ToDecimal(RPItem.Dim);
                List<StandardHoleData> RPHolePList = RPItem.HSplitInfo.SplitInfoList;
                if (RPHolePList.Count > 0)
                {
                    int FIndex = RPHolePList.FindIndex(item => !item.partialF);
                    if (FIndex != -1)
                    {
                        StandardHoleData targetHoleItem = RPHolePList[FIndex];
                        this.RPSpacingW.Value = (decimal)Math.Abs(targetHoleItem.ItemX) * 2;
                        this.RPSpacingL.Value = (decimal)Math.Abs(targetHoleItem.ItemY) * 2;
                    }
                }
            }

            //拉杆
            int spIndex = -1;
            spIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
            DItemNameType.SupportPin);
            if (spIndex == -1)
                spIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
                DItemNameType.NoNozzleMesonSupportPin);
            if (spIndex != -1)
            {
                SingleItem SPItem = ShareItemData.ItemList[spIndex];
                this.SPDim.Value = Convert.ToDecimal(SPItem.Dim);
                List<StandardHoleData> SPHolePList = SPItem.HSplitInfo.SplitInfoList;
                if (SPHolePList.Count > 0)
                {
                    int FIndex = SPHolePList.FindIndex(item => !item.partialF);
                    if (FIndex != -1)
                    {
                        StandardHoleData targetHoleItem = SPHolePList[FIndex];
                        this.SPSpacingW.Value = (decimal)Math.Abs(targetHoleItem.ItemX) * 2;
                        this.SPSpacingL.Value = (decimal)Math.Abs(targetHoleItem.ItemY) * 2;
                    }
                }
            }

            //底板螺丝
            int bpIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
            DItemNameType.BottomPanelScrew);
            if (bpIndex != -1)
            {
                SingleItem BPItem = ShareItemData.ItemList[bpIndex];
                this.BPDim.Value = Convert.ToDecimal(BPItem.Dim);
                List<StandardHoleData> BPHolePList = BPItem.HSplitInfo.SplitInfoList;
                if (BPHolePList.Count > 0)
                {
                    int FIndex = BPHolePList.FindIndex(item => !item.partialF);
                    if (FIndex != -1)
                    {
                        StandardHoleData targetHoleItem = BPHolePList[FIndex];
                        this.BPSpacingW.Value = (decimal)Math.Abs(targetHoleItem.ItemX) * 2;
                        this.BPSpacingL.Value = (decimal)Math.Abs(targetHoleItem.ItemY) * 2;
                    }
                }
            }

            //面板螺丝
            int tpIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
            DItemNameType.TopPanelScrew);
            if (tpIndex != -1)
            {
                SingleItem TPItem = ShareItemData.ItemList[tpIndex];
                this.TPDim.Value = Convert.ToDecimal(TPItem.Dim);
                List<StandardHoleData> TPHolePList = TPItem.HSplitInfo.SplitInfoList;
                if (TPHolePList.Count > 0)
                {
                    int FIndex = TPHolePList.FindIndex(item => !item.partialF);
                    if (FIndex != -1)
                    {
                        StandardHoleData targetHoleItem = TPHolePList[FIndex];
                        this.TPSpacingW.Value = (decimal)Math.Abs(targetHoleItem.ItemX) * 2;
                        this.TPSpacingL.Value = (decimal)Math.Abs(targetHoleItem.ItemY) * 2;
                    }
                }
            }

            //针板螺丝
            int epfpIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
            DItemNameType.PinBoardScrew);
            if (epfpIndex != -1)
            {
                SingleItem EPFPItem = ShareItemData.ItemList[epfpIndex];
                this.EPFPDim.Value = Convert.ToDecimal(EPFPItem.Dim);
                List<StandardHoleData> EPFPHolePList = EPFPItem.HSplitInfo.SplitInfoList;
                if (EPFPHolePList.Count > 0)
                {
                    int FIndex = EPFPHolePList.FindIndex(item => !item.partialF);
                    if (FIndex != -1)
                    {
                        StandardHoleData targetHoleItem = EPFPHolePList[FIndex];
                        this.EPFPSpacingW.Value = (decimal)Math.Abs(targetHoleItem.ItemX) * 2;
                        this.EPFPSpacingL.Value = (decimal)Math.Abs(targetHoleItem.ItemY) * 2;
                    }
                }
            }

            //方铁螺丝
            int fangtieIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
            DItemNameType.SpaceBlockScrew);
            if (fangtieIndex != -1)
            {
                SingleItem FItem = ShareItemData.ItemList[fangtieIndex];
                this.fangtieDim.Value = Convert.ToDecimal(FItem.Dim);
                List<StandardHoleData> FHolePList = FItem.HSplitInfo.SplitInfoList;
                if (FHolePList.Count > 0)
                {
                    int FIndex = FHolePList.FindIndex(item => !item.partialF);
                    if (FIndex != -1)
                    {
                        StandardHoleData targetHoleItem = FHolePList[FIndex];
                        this.fangtieSpacingW.Value = (decimal)Math.Abs(targetHoleItem.ItemX) * 2;
                        this.fangtieSpacingL.Value = (decimal)Math.Abs(targetHoleItem.ItemY) * 2;
                    }
                }
            }

            //中托边
            int egpIndex = ShareItemData.ItemList.FindIndex(item => item.ItemNameType ==
            DItemNameType.EGPPin);
            if (egpIndex != -1)
            {
                SingleItem EGPItem = ShareItemData.ItemList[egpIndex];
                this.EGPDim.Value = Convert.ToDecimal(EGPItem.Dim);
                List<StandardHoleData> EGPHolePList = EGPItem.HSplitInfo.SplitInfoList;
                if (EGPHolePList.Count > 0)
                {
                    int FIndex = EGPHolePList.FindIndex(item => !item.partialF);
                    if (FIndex != -1)
                    {
                        StandardHoleData targetHoleItem = EGPHolePList[FIndex];
                        this.EGPSpacingW.Value = (decimal)Math.Abs(targetHoleItem.ItemX) * 2;
                        this.EGPSpacingL.Value = (decimal)Math.Abs(targetHoleItem.ItemY) * 2;
                    }
                }
            }

        }

        private void FrameWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        //自动模式 保存 某个精框
        private void AutoTypeSaveOneBoardFrame()
        {
            List<BoardSectionItem> targetList = AssemblyShareData.AUIBlueSectionList;
            targetList[operIndex].frameItem.FSType = FrameSplitType.S;
            targetList[operIndex].frameItem.FrameLength = Convert.ToDouble(this.frameLBox.Value);
            targetList[operIndex].frameItem.FrameWidth = Convert.ToDouble(this.frameWBox.Value);
            targetList[operIndex].frameItem.FrameDepth = Convert.ToDouble(this.frameDBox.Value);

            targetList[operIndex].frameItem.TL_Radius = Convert.ToDouble(this.TLValue.Value);
            targetList[operIndex].frameItem.TR_Radius = Convert.ToDouble(this.TRValue.Value);
            targetList[operIndex].frameItem.BL_Radius = Convert.ToDouble(this.BLValue.Value);
            targetList[operIndex].frameItem.BR_Radius = Convert.ToDouble(this.BRValue.Value);

            string TLText = this.TLType.Text;
            if (TLText.Equals("R"))
                targetList[operIndex].frameItem.TL_Type = AngleType.COR;
            if (TLText.Equals("避空"))
                targetList[operIndex].frameItem.TL_Type = AngleType.BK;
            if (TLText.Equals("X避空"))
                targetList[operIndex].frameItem.TL_Type = AngleType.XBK;
            if (TLText.Equals("Y避空"))
                targetList[operIndex].frameItem.TL_Type = AngleType.YBK;

            string TRText = this.TRType.Text;
            if (TRText.Equals("R"))
                targetList[operIndex].frameItem.TR_Type = AngleType.COR;
            if (TRText.Equals("避空"))
                targetList[operIndex].frameItem.TR_Type = AngleType.BK;
            if (TRText.Equals("X避空"))
                targetList[operIndex].frameItem.TR_Type = AngleType.XBK;
            if (TRText.Equals("Y避空"))
                targetList[operIndex].frameItem.TR_Type = AngleType.YBK;

            string BLText = this.BLType.Text;
            if (BLText.Equals("R"))
                targetList[operIndex].frameItem.BL_Type = AngleType.COR;
            if (BLText.Equals("避空"))
                targetList[operIndex].frameItem.BL_Type = AngleType.BK;
            if (BLText.Equals("X避空"))
                targetList[operIndex].frameItem.BL_Type = AngleType.XBK;
            if (BLText.Equals("Y避空"))
                targetList[operIndex].frameItem.BL_Type = AngleType.YBK;

            string BRText = this.BRType.Text;
            if (BRText.Equals("R"))
                targetList[operIndex].frameItem.BR_Type = AngleType.COR;
            if (BRText.Equals("避空"))
                targetList[operIndex].frameItem.BR_Type = AngleType.BK;
            if (BRText.Equals("X避空"))
                targetList[operIndex].frameItem.BR_Type = AngleType.XBK;
            if (BRText.Equals("Y避空"))
                targetList[operIndex].frameItem.BR_Type = AngleType.YBK;

            RecognizeUtils.SetFrameItemTig(operIndex);
            RecognizeUtils.AutoTypeCRUIAfterRecognizeEvent(RenderType.AfterRecoginzeFrame);

            this.Hide();
        }

        //手动模式 保存 某个精框
        private void MTypeSaveOneBoardFrame()
        {
            List<ManualBoardItem> targetList = AssemblyShareData.MSectionList;
            targetList[operIndex].frameItem.FrameLength = Convert.ToDouble(this.frameLBox.Value);
            targetList[operIndex].frameItem.FrameWidth = Convert.ToDouble(this.frameWBox.Value);
            targetList[operIndex].frameItem.FrameDepth = Convert.ToDouble(this.frameDBox.Value);

            targetList[operIndex].frameItem.TL_Radius = Convert.ToDouble(this.TLValue.Value);
            targetList[operIndex].frameItem.TR_Radius = Convert.ToDouble(this.TRValue.Value);
            targetList[operIndex].frameItem.BL_Radius = Convert.ToDouble(this.BLValue.Value);
            targetList[operIndex].frameItem.BR_Radius = Convert.ToDouble(this.BRValue.Value);

            string TLText = this.TLType.Text;
            if (TLText.Equals("R"))
                targetList[operIndex].frameItem.TL_Type = AngleType.COR;
            if (TLText.Equals("避空"))
                targetList[operIndex].frameItem.TL_Type = AngleType.BK;
            if (TLText.Equals("X避空"))
                targetList[operIndex].frameItem.TL_Type = AngleType.XBK;
            if (TLText.Equals("Y避空"))
                targetList[operIndex].frameItem.TL_Type = AngleType.YBK;

            string TRText = this.TRType.Text;
            if (TRText.Equals("R"))
                targetList[operIndex].frameItem.TR_Type = AngleType.COR;
            if (TRText.Equals("避空"))
                targetList[operIndex].frameItem.TR_Type = AngleType.BK;
            if (TRText.Equals("X避空"))
                targetList[operIndex].frameItem.TR_Type = AngleType.XBK;
            if (TRText.Equals("Y避空"))
                targetList[operIndex].frameItem.TR_Type = AngleType.YBK;

            string BLText = this.BLType.Text;
            if (BLText.Equals("R"))
                targetList[operIndex].frameItem.BL_Type = AngleType.COR;
            if (BLText.Equals("避空"))
                targetList[operIndex].frameItem.BL_Type = AngleType.BK;
            if (BLText.Equals("X避空"))
                targetList[operIndex].frameItem.BL_Type = AngleType.XBK;
            if (BLText.Equals("Y避空"))
                targetList[operIndex].frameItem.BL_Type = AngleType.YBK;

            string BRText = this.BRType.Text;
            if (BRText.Equals("R"))
                targetList[operIndex].frameItem.BR_Type = AngleType.COR;
            if (BRText.Equals("避空"))
                targetList[operIndex].frameItem.BR_Type = AngleType.BK;
            if (BRText.Equals("X避空"))
                targetList[operIndex].frameItem.BR_Type = AngleType.XBK;
            if (BRText.Equals("Y避空"))
                targetList[operIndex].frameItem.BR_Type = AngleType.YBK;

            RecognizeUtils.SetFrameItemTig(operIndex);
            this.Hide();
            MTypeRenderAboutFrameUIAction?.Invoke(operIndex);
        }

        //Save Frame 
        private void ComfirmBtn_Click(object sender, EventArgs e)
        {
            if (AssemblyShareData.operType == CustomOperPatternType.AUTO)
                AutoTypeSaveOneBoardFrame();
            if (AssemblyShareData.operType == CustomOperPatternType.MANUAL)
                MTypeSaveOneBoardFrame();
        }

        private void resetAllC_Click(object sender, EventArgs e)
        {
            //数据复位
            operFItem = (FrameItem)ConvertUtils.DeepCopyObject(originOperFrameInfo);
            SetFrameBaseUIData();
        }

        private void cAllType_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetAllCTypeOper();
            AutoSetDefaultAngleValueLogic();
        }

        //联动全部设置CType参数
        private void SetAllCTypeOper()
        {
            //设置UI
            int typeIndex = this.cAllType.SelectedIndex;
            this.TLType.SelectedIndex = typeIndex;
            this.TRType.SelectedIndex = typeIndex;
            this.BLType.SelectedIndex = typeIndex;
            this.BRType.SelectedIndex = typeIndex;

            //设置值
            List<BoardSectionItem> targetList = AssemblyShareData.AUIBlueSectionList;
            FrameItem operFItem = targetList[operIndex].frameItem;
            string cTypeText = this.cAllType.Text;
            if (cTypeText.Equals("避空"))
            {
                operFItem.TL_Type = AngleType.BK;
                operFItem.TR_Type = AngleType.BK;
                operFItem.BL_Type = AngleType.BK;
                operFItem.BR_Type = AngleType.BK;
            }
            if (cTypeText.Equals("X避空"))
            {
                operFItem.TL_Type = AngleType.XBK;
                operFItem.TR_Type = AngleType.XBK;
                operFItem.BL_Type = AngleType.XBK;
                operFItem.BR_Type = AngleType.XBK;
            }
            if (cTypeText.Equals("Y避空"))
            {
                operFItem.TL_Type = AngleType.YBK;
                operFItem.TR_Type = AngleType.YBK;
                operFItem.BL_Type = AngleType.YBK;
                operFItem.BR_Type = AngleType.YBK;
            }
            if (cTypeText.Equals("R"))
            {
                operFItem.TL_Type = AngleType.COR;
                operFItem.TR_Type = AngleType.COR;
                operFItem.BL_Type = AngleType.COR;
                operFItem.BR_Type = AngleType.COR;
            }
        }

        private void ResetErrorTigAndUI()
        {
            colErrorList = new List<string>();
            PitchColErrorTigLablel.Text = "暂无";
        }

        private void InitFrameLayerLogic()
        {
            Boolean LayerF = CADAuxiliaryUtils.JudegLayerExist(frameLayerName);
            if(!LayerF)
                CADAuxiliaryUtils.CreateLayerByName(frameLayerName);
            //CADAuxiliaryUtils.DeleteEntityOnLayer(frameLayerName);
        }

        //推算出框图绘制的合适坐标 暂定 后续优化位置
        private void GetFrameFitCon()
        {
            PictureFrameCondiInfo UserSelectCondiInfo = ComputedUtils.dwgSelectAreaCondication(RecognizeUtils.TopViewPicCondiInfo,
                RecognizeUtils.AssemblyViewPicCondiInfo);
            var moveY = UserSelectCondiInfo.TY;
            var moveLX = UserSelectCondiInfo.LX;
            var moveRX = UserSelectCondiInfo.RX;
            if(moveY != null && moveLX != null && moveRX != null)
            {
                double FL = Convert.ToDouble(this.frameLBox.Value);
                double drawX = ((double)moveLX + (double)moveRX) / 2;
                double drawY = (double)moveY + FL / 2 + 300;
                RefOPoint.X = drawX;
                RefOPoint.Y = drawY;
            }
        }

        //清除整个精框示意图
        private void ClearDWGFrameFeature()
        {
            Document doc = ZwSoft.ZwCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            DocumentLock m_DocumentLock = doc.LockDocument();
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord btr = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                    foreach (ObjectId objId in btr)
                    {
                        if (objId != ObjectId.Null)
                        {
                            Entity entToErase = (Entity)tr.GetObject(objId, OpenMode.ForWrite);
                            Boolean F = entToErase.Layer.Equals(frameLayerName);
                            if (F)
                                entToErase.Erase();
                        }
                    }
                    tr.Commit();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (m_DocumentLock != null)
                    m_DocumentLock.Dispose();
            }

           
        }

        //TO EDIT 根据参数 绘图逻辑
        private void ArgDraw()
        {
            InitFrameLayerLogic();
            ClearDWGFrameFeature();
            GetFrameFitCon();
            ResetErrorTigAndUI();
            DrawOutSideBoard();
            DrawHoleByDyncArc();
            double FL = Convert.ToDouble(this.frameLBox.Value);
            double FW = Convert.ToDouble(this.frameWBox.Value);
            if(FL > 0 && FW > 0)
            {
                DrawArgFrame();
                CheckCol();
            }
        }

        private void CheckCol()
        {
            CheckFrameHolePhaseCol();
            CheckBoardRPHoleCol();
            if (AssemblyShareData.operType == CustomOperPatternType.AUTO)
            {
                AMTypeCheckFrameBoardPhasseCol("A");
                double PL = APageOperBItem.PlateLength;
                double PW = APageOperBItem.PlateWidth;
                CheckBoardHolePhaseCol(PL,PW);
            }
            if (AssemblyShareData.operType == CustomOperPatternType.MANUAL)
            {
                AMTypeCheckFrameBoardPhasseCol("M");
                double PL = MPageOperBItem.PlateLength;
                double PW = MPageOperBItem.PlateWidth;
                CheckBoardHolePhaseCol(PL, PW);
            }
            if (HasColErrorF)
            {
                if(colErrorList.Count > 0)
                {
                    string showWord = "";
                    colErrorList.ForEach(item => showWord += item);
                    PitchColErrorTigLablel.Text = showWord;
                }
            }
        }

        private void DrawOutSideBoard()
        {
            if (AssemblyShareData.operType == CustomOperPatternType.AUTO)
                AutoTypeDrawFrameLogic();
            if (AssemblyShareData.operType == CustomOperPatternType.MANUAL)
                MTypeDrawFrameLogic();
        }

        //自动模式 板 绘图
        private void AutoTypeDrawFrameLogic()
        {
            List<BoardSectionItem> targetList = AssemblyShareData.AUIBlueSectionList;
            BoardSectionItem boardItem = targetList[operIndex];
            double boardL = boardItem.PlateLength;
            double boardW = boardItem.PlateWidth;
            GsViewL = boardL;
            GsViewW = boardW;
            DWGControlDrawOutSidBoard(boardW, boardL);
        }

        //手动模式 板 绘图
        private void MTypeDrawFrameLogic()
        {
            List<ManualBoardItem> targetList = AssemblyShareData.MSectionList;
            ManualBoardItem boardItem = targetList[operIndex];
            double boardW = boardItem.PlateWidth;
            double boardL = boardItem.PlateLength;
            GsViewL = boardL;
            GsViewW = boardW;
            DWGControlDrawOutSidBoard(boardW, boardL);
        }



        private void DWGControlDrawOutSidBoard(double boardW, double boardL)
        {
            Document doc = ZwSoft.ZwCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                using (DocumentLock DocLock = doc.LockDocument())
                {
                    double halfW = boardW / 2;
                    double halfL = boardL / 2;
                    Point3d TLPoint = new Point3d(RefOPoint.X - halfW, RefOPoint.Y + halfL, 0);
                    Point3d TRPoint = new Point3d(RefOPoint.X + halfW, RefOPoint.Y + halfL, 0);
                    Point3d BLPoint = new Point3d(RefOPoint.X - halfW, RefOPoint.Y - halfL, 0);
                    Point3d BRPoint = new Point3d(RefOPoint.X + halfW, RefOPoint.Y - halfL, 0);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
                        LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(lt[frameLayerName], OpenMode.ForWrite);
                        
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                        Line line1 = new Line(TLPoint, BLPoint);
                        Line line2 = new Line(BLPoint, BRPoint);
                        Line line3 = new Line(BRPoint, TRPoint);
                        Line line4 = new Line(TRPoint, TLPoint);
                        line1.LayerId = ltr.ObjectId;
                        line2.LayerId = ltr.ObjectId;
                        line3.LayerId = ltr.ObjectId;
                        line4.LayerId = ltr.ObjectId;

                        btr.AppendEntity(line1);
                        btr.AppendEntity(line2);
                        btr.AppendEntity(line3);
                        btr.AppendEntity(line4);
                        tr.AddNewlyCreatedDBObject(line1, true);
                        tr.AddNewlyCreatedDBObject(line2, true);
                        tr.AddNewlyCreatedDBObject(line3, true);
                        tr.AddNewlyCreatedDBObject(line4, true);

                        tr.Commit();
                    }
                    InitDrawingControl(doc, db);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }


        private void DWGControlDrawHole(double CircleX, double CircleY,double Radius)
        {
            Document doc = ZwSoft.ZwCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                using (DocumentLock DocLock = doc.LockDocument())
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
                        LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(lt[frameLayerName], OpenMode.ForWrite);
                        Point3d CPoint = new Point3d(RefOPoint.X + CircleX, RefOPoint.Y + CircleY, 0);
                        Circle circle = new Circle(CPoint, Vector3d.ZAxis, Radius);
                        circle.LayerId = ltr.ObjectId;
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        btr.AppendEntity(circle);
                        tr.AddNewlyCreatedDBObject(circle, true);
                        tr.Commit();

                    }
                    InitDrawingControl(doc, db);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void DWGDrawFrameLine(CAD_Point argPoint1, CAD_Point argPoint2)
        {
            Document doc = ZwSoft.ZwCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                using (DocumentLock DocLock = doc.LockDocument())
                {
                    Point3d Point1 = new Point3d(argPoint1.X, argPoint1.Y, 0);
                    Point3d Point2 = new Point3d(argPoint2.X, argPoint2.Y, 0);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
                        LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(lt[frameLayerName], OpenMode.ForWrite);
                        Line line = new Line(Point1, Point2);
                        line.LayerId = ltr.ObjectId;
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        btr.AppendEntity(line);
                        tr.AddNewlyCreatedDBObject(line, true);
                        tr.Commit();
                    }
                    InitDrawingControl(doc, db);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        static double DegreesToRadians(double degrees)
        {
            return degrees * (Math.PI / 180);
        }

        private void DWGDrawFrameArc(CAD_Point arcCPoint,double startAngle,double endAngle, double Radius)
        {
            Document doc = ZwSoft.ZwCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                using (DocumentLock DocLock = doc.LockDocument())
                {
                    Point3d center = new Point3d(arcCPoint.X, arcCPoint.Y, 0);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
                        LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(lt[frameLayerName], OpenMode.ForWrite);
                        double startRad = DegreesToRadians(startAngle);
                        double endRad = DegreesToRadians(endAngle);
                        Arc arc = new Arc(center, Radius, startRad, endRad);
                        arc.LayerId = ltr.ObjectId;
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        btr.AppendEntity(arc);
                        tr.AddNewlyCreatedDBObject(arc, true);
                        tr.Commit();

                    }
                    InitDrawingControl(doc, db);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        //绘制1组孔
        private void DrawHoleGroup(double CircleSize, List<StandardHoleData> targetList)
        {
            Document doc = ZwSoft.ZwCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            double radius = CircleSize / 2;
            if (targetList.Count > 0)
            {
                for (int i = 0; i < targetList.Count; i++)
                {
                    double ItemX = targetList[i].ItemX;
                    double ItemY = targetList[i].ItemY;
                    double CircleX = ItemX;
                    double CircleY = ItemY;
                    DWGControlDrawHole(CircleX, CircleY, radius);
                }
            }
        }

        private void DrawArgFrame()
        {
            framePhaseCol.angList = new List<ComputedAngleRes>();
            double HalfFW = (double)this.frameWBox.Value / 2;
            double HalfFL = (double)this.frameLBox.Value / 2;

            //Vertex Angle Center split Relative coordinates
            CAD_Point RefPCenter = new CAD_Point(RefOPoint.X, RefOPoint.Y,0);

            AngleType TLType = GetAngleTypeByStr(this.TLType.Text);
            AngleType TRType = GetAngleTypeByStr(this.TRType.Text);
            AngleType BLType = GetAngleTypeByStr(this.BLType.Text);
            AngleType BRType = GetAngleTypeByStr(this.BRType.Text);
            int BLVDim = (int)this.BLValue.Value;
            int BRVDim = (int)this.BRValue.Value;
            int TLVDim = (int)this.TLValue.Value;
            int TRVDim = (int)this.TRValue.Value;

            CAD_Point TLP = new CAD_Point( -HalfFW,HalfFL,0);
            CAD_Point TRP = new CAD_Point(HalfFW, HalfFL,0);
            CAD_Point BLP = new CAD_Point( -HalfFW,-HalfFL,0);
            CAD_Point BRP = new CAD_Point(HalfFW,-HalfFL,0);

            //After Computed
            ComputedAngleRes TLAngleRes = null;
            ComputedAngleRes TRAngleRes = null;
            ComputedAngleRes BLAngleRes = null;
            ComputedAngleRes BRAngleRes = null;

            AngleDir TLDir = AngleDir.TL;
            AngleDir TRDir = AngleDir.TR;
            AngleDir BLDir = AngleDir.BL;
            AngleDir BRDir = AngleDir.BR;
                        
            AngleArg TLArg = new AngleArg();
            TLArg.angleDir = TLDir;
            TLArg.angleType = TLType;
            TLArg.Dim = TLVDim;
            if (TLArg.Dim > 0)
            {
                TLAngleRes = ComputedFrameCor(RefPCenter, TLP, TLArg);
                //CloseForeCastDim(TLAngleRes);
                framePhaseCol.angList.Add(TLAngleRes);
            }
            else
            {
                TLAngleRes = ZeroDimFrameCor(RefPCenter, TLP, TLArg);
                framePhaseCol.angList.Add(TLAngleRes);
            }
            
            AngleArg TRArg = new AngleArg();
            TRArg.angleDir = TRDir;
            TRArg.angleType = TRType;
            TRArg.Dim = TRVDim;
            if (TRArg.Dim > 0)
            {
                TRAngleRes = ComputedFrameCor(RefPCenter, TRP, TRArg);
                framePhaseCol.angList.Add(TRAngleRes);
            }
            else
            {
                TRAngleRes = ZeroDimFrameCor(RefPCenter, TRP, TRArg);
                framePhaseCol.angList.Add(TRAngleRes);
            }

            //BL Default Standard 
            AngleArg BLArg = new AngleArg();
            BLArg.angleDir = BLDir;
            BLArg.angleType = BLType;
            BLArg.Dim = BLVDim;
            if (BLArg.Dim > 0)
            {
                BLAngleRes = ComputedFrameCor(RefPCenter, BLP, BLArg);
                framePhaseCol.angList.Add(BLAngleRes);
            }
            else
            {
                BLAngleRes = ZeroDimFrameCor(RefPCenter, BLP, BLArg);
                framePhaseCol.angList.Add(BLAngleRes);
            }

            AngleArg BRArg = new AngleArg();
            BRArg.angleDir = BRDir;
            BRArg.angleType = BRType;
            BRArg.Dim = BRVDim;
            if (BRArg.Dim > 0)
            {
                BRAngleRes = ComputedFrameCor(RefPCenter, BRP, BRArg);
                framePhaseCol.angList.Add(BRAngleRes);
            }
            else
            {
                BRAngleRes = ZeroDimFrameCor(RefPCenter, BRP, BRArg);
                framePhaseCol.angList.Add(BRAngleRes);
            }

            CAD_Point T1 = TLAngleRes.Point1;
            CAD_Point T2 = TRAngleRes.Point2;
            CAD_Point B1 = BRAngleRes.Point1;
            CAD_Point B2 = BLAngleRes.Point2;
            CAD_Point L1 = BLAngleRes.Point1;
            CAD_Point L2 = TLAngleRes.Point2;
            CAD_Point R1 = TRAngleRes.Point1;
            CAD_Point R2 = BRAngleRes.Point2;

            DWGDrawFrameLine(T1, T2);//T
            DWGDrawFrameLine(L1, L2); //L
            DWGDrawFrameLine(B1, B2);//B
            DWGDrawFrameLine(R1, R2);//R
        }

        private ComputedAngleRes ZeroDimFrameCor(CAD_Point skewPoint, CAD_Point framePoint, AngleArg cornerData)
        {
            ComputedAngleRes Res = new ComputedAngleRes();
            AngleDir targetDir = cornerData.angleDir;
            switch (targetDir)
            {
                case AngleDir.TL:
                    Res.Point1.X = skewPoint.X + framePoint.X;
                    Res.Point1.Y = skewPoint.Y + framePoint.Y;
                    Res.Point2.X = skewPoint.X + framePoint.X;
                    Res.Point2.Y = skewPoint.Y + framePoint.Y;
                    Res.FAPoint.X = skewPoint.X + framePoint.X;
                    Res.FAPoint.Y = skewPoint.Y + framePoint.Y;
                    break;
                case AngleDir.BL:
                    Res.Point1.X = skewPoint.X + framePoint.X;
                    Res.Point1.Y = skewPoint.Y + framePoint.Y;
                    Res.Point2.X = skewPoint.X + framePoint.X;
                    Res.Point2.Y = skewPoint.Y + framePoint.Y;
                    Res.FAPoint.X = skewPoint.X + framePoint.X;
                    Res.FAPoint.Y = skewPoint.Y + framePoint.Y;
                    break;
                case AngleDir.TR:
                    Res.Point1.X = skewPoint.X + framePoint.X;
                    Res.Point1.Y = skewPoint.Y + framePoint.Y;
                    Res.Point2.X = skewPoint.X + framePoint.X;
                    Res.Point2.Y = skewPoint.Y + framePoint.Y;
                    Res.FAPoint.X = skewPoint.X + framePoint.X;
                    Res.FAPoint.Y = skewPoint.Y + framePoint.Y;
                    break;
                case AngleDir.BR:
                    Res.Point1.X = skewPoint.X + framePoint.X;
                    Res.Point1.Y = skewPoint.Y + framePoint.Y;
                    Res.Point2.X = skewPoint.X + framePoint.X;
                    Res.Point2.Y = skewPoint.Y + framePoint.Y;
                    Res.FAPoint.X = skewPoint.X + framePoint.X;
                    Res.FAPoint.Y = skewPoint.Y + framePoint.Y;
                    break;
            }
            return Res;
        }
        private ComputedAngleRes ComputedFrameCor(CAD_Point skewPoint,CAD_Point framePoint, AngleArg cornerData)
        {
            ComputedAngleRes Res = new ComputedAngleRes();
            Res.FAPoint.X = skewPoint.X + framePoint.X;
            Res.FAPoint.Y = skewPoint.Y + framePoint.Y;
            CAD_Point[] cornerPoints = new CAD_Point[2];
            cornerPoints[0] = new CAD_Point();
            cornerPoints[1] = new CAD_Point();
            if (cornerData.angleType == AngleType.COR)
            {
                double startang = (framePoint.X > 0) ? ((framePoint.Y > 0) ? 0 : 270) : ((framePoint.Y > 0) ? 90 : 180);
                double endang = (framePoint.X > 0) ? ((framePoint.Y > 0) ? 90 : 0) : ((framePoint.Y > 0) ? 180 : 270);
                double x = (framePoint.X > 0) ? framePoint.X - cornerData.Dim : framePoint.X + cornerData.Dim;
                double y = (framePoint.Y > 0) ? framePoint.Y - cornerData.Dim : framePoint.Y + cornerData.Dim;
                double readX = skewPoint.X + x;
                double readY = skewPoint.Y + y;
                Res.startang = startang;
                Res.endang = endang;
                Res.ArcCPoint.X = x;
                Res.ArcCPoint.Y = y;
                Res.Dim = cornerData.Dim;
                DWGDrawFrameArc(new CAD_Point(readX, readY, 0), startang, endang, cornerData.Dim);
                switch (startang)
                {
                    case 0:
                        cornerPoints[0].X = framePoint.X;
                        cornerPoints[0].Y = framePoint.Y - cornerData.Dim;
                        cornerPoints[1].X = framePoint.X - cornerData.Dim;
                        cornerPoints[1].Y = framePoint.Y;
                        Res.Point1 = cornerPoints[0];
                        Res.Point2 = cornerPoints[1];
                        break;
                    case 90:
                        cornerPoints[0].X = framePoint.X + cornerData.Dim;
                        cornerPoints[0].Y = framePoint.Y;
                        cornerPoints[1].X = framePoint.X;
                        cornerPoints[1].Y = framePoint.Y - cornerData.Dim;
                        Res.Point1 = cornerPoints[0];
                        Res.Point2 = cornerPoints[1];
                        break;
                    case 180:
                        cornerPoints[0].X = framePoint.X;
                        cornerPoints[0].Y = framePoint.Y + cornerData.Dim;
                        cornerPoints[1].X = framePoint.X + cornerData.Dim;
                        cornerPoints[1].Y = framePoint.Y;
                        Res.Point1 = cornerPoints[0];
                        Res.Point2 = cornerPoints[1];
                        break;
                    case 270:
                        cornerPoints[0].X = framePoint.X - cornerData.Dim;
                        cornerPoints[0].Y = framePoint.Y;
                        cornerPoints[1].X = framePoint.X;
                        cornerPoints[1].Y = framePoint.Y + cornerData.Dim;
                        Res.Point1 = cornerPoints[0];
                        Res.Point2 = cornerPoints[1];
                        break;
                }
            }
            
            if (cornerData.angleType == AngleType.BK)
            {
                double startang = (framePoint.X > 0) ? ((framePoint.Y > 0) ? 315 : 225) : ((framePoint.Y > 0) ? 45 : 135);
                double endang = (framePoint.X > 0) ? ((framePoint.Y > 0) ? 135 : 45) : ((framePoint.Y > 0) ? 225 : 315);
                double x = (framePoint.X > 0) ? framePoint.X - cornerData.Dim * 0.707 : framePoint.X + cornerData.Dim * 0.707;
                double y = (framePoint.Y > 0) ? framePoint.Y - cornerData.Dim * 0.707 : framePoint.Y + cornerData.Dim * 0.707;
                Res.startang = startang;
                Res.endang = endang;
                double readX = skewPoint.X + x;
                double readY = skewPoint.Y + y;
                Res.ArcCPoint.X = x;
                Res.ArcCPoint.Y = y;
                Res.Dim = cornerData.Dim;
                DWGDrawFrameArc(new CAD_Point(readX, readY, 0), startang, endang, cornerData.Dim);
                switch (startang)
                {
                    case 315:
                        cornerPoints[0].X = framePoint.X;
                        cornerPoints[0].Y = framePoint.Y - cornerData.Dim * 1.414;
                        cornerPoints[1].X = framePoint.X - cornerData.Dim * 1.414;
                        cornerPoints[1].Y = framePoint.Y;
                        Res.Point1 = cornerPoints[0];
                        Res.Point2 = cornerPoints[1];
                        break;
                    case 45:
                        cornerPoints[0].X = framePoint.X + cornerData.Dim * 1.414;
                        cornerPoints[0].Y = framePoint.Y;
                        cornerPoints[1].X = framePoint.X;
                        cornerPoints[1].Y = framePoint.Y - cornerData.Dim * 1.414;
                        Res.Point1 = cornerPoints[0];
                        Res.Point2 = cornerPoints[1];
                        break;
                    case 135:
                        cornerPoints[0].X = framePoint.X;
                        cornerPoints[0].Y = framePoint.Y + cornerData.Dim * 1.414;
                        cornerPoints[1].X = framePoint.X + cornerData.Dim * 1.414;
                        cornerPoints[1].Y = framePoint.Y;
                        Res.Point1 = cornerPoints[0];
                        Res.Point2 = cornerPoints[1];
                        break;
                    case 225:
                        cornerPoints[0].X = framePoint.X - cornerData.Dim * 1.414;
                        cornerPoints[0].Y = framePoint.Y;
                        cornerPoints[1].X = framePoint.X;
                        cornerPoints[1].Y = framePoint.Y + cornerData.Dim * 1.414;
                        Res.Point1 = cornerPoints[0];
                        Res.Point2 = cornerPoints[1];
                        break;
                }
            }

            if (cornerData.angleType == AngleType.XBK)
            {
                AngleDir ADir = cornerData.angleDir;
                double startang = 0;
                double endang = 0;
                double x = 0;
                double y = 0;
                if (framePoint.X < 0)
                {
                    startang = 90;
                    endang = 270;
                }
                if (framePoint.X > 0)
                {
                    startang = 270;
                    endang = 90;
                }
                if (framePoint.Y > 0)
                    y = -cornerData.Dim;
                if (framePoint.Y < 0)
                    y = cornerData.Dim;
                double readX = framePoint.X + skewPoint.X + x;
                double readY = framePoint.Y + skewPoint.Y + y;
                Res.startang = startang;
                Res.endang = endang;
                Res.ArcCPoint.X = x;
                Res.ArcCPoint.Y = y;
                Res.Dim = cornerData.Dim;
                DWGDrawFrameArc(new CAD_Point(readX, readY, 0), startang, endang, cornerData.Dim);
                cornerPoints[0].X = framePoint.X;
                cornerPoints[1].X = framePoint.X;
                if (ADir == AngleDir.BR)
                {
                    cornerPoints[0].Y = framePoint.Y;
                    cornerPoints[1].Y = framePoint.Y + cornerData.Dim * 2;
                }
                if (ADir == AngleDir.BL)
                {
                    cornerPoints[0].Y = framePoint.Y + cornerData.Dim * 2;
                    cornerPoints[1].Y = framePoint.Y;
                }
                if (ADir == AngleDir.TL)
                {
                    cornerPoints[0].Y = framePoint.Y;
                    cornerPoints[1].Y = framePoint.Y - cornerData.Dim * 2;
                }
                if (ADir == AngleDir.TR)
                {
                    cornerPoints[0].Y = framePoint.Y - cornerData.Dim * 2;
                    cornerPoints[1].Y = framePoint.Y;
                }
                Res.Point1 = cornerPoints[0];
                Res.Point2 = cornerPoints[1];
            }

            if (cornerData.angleType == AngleType.YBK)
            {
                AngleDir ADir = cornerData.angleDir;
                double startang = 0;
                double endang = 0;
                double x = 0;
                double y = 0;
                if (framePoint.Y > 0)
                {
                    startang = 0;
                    endang = 180;
                }
                if (framePoint.Y < 0)
                {
                    startang = 180;
                    endang = 0;
                }
                if (framePoint.X > 0)
                    x = -cornerData.Dim;
                if (framePoint.X < 0)
                    x = cornerData.Dim;
                double readX = framePoint.X + skewPoint.X + x;
                double readY = framePoint.Y + skewPoint.Y + y;
                Res.startang = startang;
                Res.endang = endang;
                Res.ArcCPoint.X = x;
                Res.ArcCPoint.Y = y;
                Res.Dim = cornerData.Dim;
                DWGDrawFrameArc(new CAD_Point(readX, readY, 0), startang, endang, cornerData.Dim);
                cornerPoints[0].Y = framePoint.Y;
                cornerPoints[1].Y = framePoint.Y;
                if (ADir == AngleDir.BR)
                {
                    cornerPoints[0].X = framePoint.X - cornerData.Dim * 2;
                    cornerPoints[1].X = framePoint.X;
                }
                if (ADir == AngleDir.BL)
                {
                    cornerPoints[0].X = framePoint.X;
                    cornerPoints[1].X = framePoint.X + cornerData.Dim * 2;
                }
   
                if (ADir == AngleDir.TL)
                {
                    cornerPoints[0].X = framePoint.X + cornerData.Dim * 2;
                    cornerPoints[1].X = framePoint.X;
                }
                if (ADir == AngleDir.TR)
                {
                    cornerPoints[0].X = framePoint.X;
                    cornerPoints[1].X = framePoint.X - cornerData.Dim * 2;
                }
                Res.Point1 = cornerPoints[0];
                Res.Point2 = cornerPoints[1];
            }
            Res.angleType = cornerData.angleType;
            Res.angleDir = cornerData.angleDir;
            Res.Point1.X = skewPoint.X + Res.Point1.X; 
            Res.Point1.Y = skewPoint.Y + Res.Point1.Y;
            Res.Point2.X = skewPoint.X + Res.Point2.X;
            Res.Point2.Y = skewPoint.Y + Res.Point2.Y;
            return Res;
        }

        //检测框&板 是否相崩
        private void AMTypeCheckFrameBoardPhasseCol(string checkType = null)
        {
            Boolean ColF = false;
            StringBuilder stringBuilder = new StringBuilder();
            double? bpxv = null;
            double? bpyv = null;
            if(checkType.Equals("A"))
            {
                bpxv = APageOperBItem.PlateWidth / 2;
                bpyv = APageOperBItem.PlateLength / 2;
            }
            if (checkType.Equals("M"))
            {
                bpxv = MPageOperBItem.PlateWidth / 2;
                bpyv = MPageOperBItem.PlateLength / 2;
            }
            double fpxv = (double)(frameWBox.Value / 2);
            double fpyv = (double)(frameLBox.Value / 2);
            Boolean F1 = fpxv >= bpxv;
            Boolean F2 = fpyv >= bpyv;
            if (F1)
            {
                ColF = true;
                string tig = "框与板宽度相崩,请检查数据! ";
                stringBuilder.Append(tig);
            }
            if (F2)
            {
                ColF = true;
                string tig = "框与板长度相崩,请检查数据! ";
                stringBuilder.Append(tig);
            }
            if (ColF)
            {
                HasColErrorF = true;
                colErrorList.Add(stringBuilder.ToString());
            }
        }

        //Check Frame GP SP RP Col TO EDIT 
        private void CheckFrameHolePhaseCol()
        {
            Boolean ColF = false;
            StringBuilder stringBuilder = new StringBuilder();
            List<ComputedAngleRes> angList = framePhaseCol.angList;
            if (angList.Count > 0)
            {
                for (int i = 0; i < angList.Count; i++)
                {
                    if (GPList.Count > 0)
                    {
                        foreach (var gpItem in GPList)
                        {
                            Boolean F = PhaseColSecure(angList[i], gpItem,"GP");
                            if(F)
                            {
                                string cd = ChineseDir(angList[i].angleDir);
                                cd = ChineseType(angList[i].angleType, cd);
                                cd += "与导柱相崩! ";
                                ColF = true;
                                stringBuilder.Append(cd);
                            }
                        }
                    }

                    if (SPList.Count > 0)
                    {
                        foreach (var spItem in SPList)
                        {
                            Boolean F = PhaseColSecure(angList[i], spItem,"SP");
                            if (F)
                            {
                                string cd = ChineseDir(angList[i].angleDir);
                                cd = ChineseType(angList[i].angleType, cd);
                                cd += "与拉杆相崩! ";
                                ColF = true;
                                stringBuilder.Append(cd);
                            }
                        }
                    }

                    if (RPList.Count > 0)
                    {
                        foreach (var rpItem in RPList)
                        {
                            Boolean F = PhaseColSecure(angList[i], rpItem);
                            if (F)
                            {
                                string cd = ChineseDir(angList[i].angleDir);
                                cd = ChineseType(angList[i].angleType, cd);
                                cd += "与回针相崩! ";
                                ColF = true;
                                stringBuilder.Append(cd);
                            }
                        }
                    }
                }
            }
            if (ColF)
            {
                HasColErrorF = true;
                colErrorList.Add(stringBuilder.ToString());
            }
        }

        //Line Y & Circle CenterPoint Col Rule
        private Boolean LyCCol(double by,StandardHoleData RPHole)
        {
            double dim = RPHole.ItemDim;
            double holeY = RPHole.ItemY;
            double diff = Math.Abs(by - holeY);
            Boolean F = diff - dim <= 0.1;
            return F;
        }

        //GP SP & Board Col
        private void CheckBoardHolePhaseCol(double PL,double PW)
        {
            Boolean ColF = false;
            StringBuilder stringBuilder = new StringBuilder();
            Boolean GPLF = (PL - (double)this.GPSpacingL.Value) / 2 < (double)this.GPDim.Value;
            Boolean GPWF = (PW - (double)this.GPSpacingW.Value) / 2 < (double)this.GPDim.Value;
            Boolean SPLF = (PL - (double)this.SPSpacingL.Value) / 2 < (double)this.SPDim.Value;
            Boolean SPWF = (PW - (double)this.SPSpacingL.Value) / 2 < (double)this.SPDim.Value;
            if(GPLF || GPWF)
            {
                ColF = true;
                string tig = "导柱到板边太近,请检查数据! ";
                stringBuilder.Append(tig);
            }
            if (SPLF || SPWF)
            {
                ColF = true;
                string tig = "拉杆到板边太近,请检查数据! ";
                stringBuilder.Append(tig);
            }
            if (ColF)
            {
                HasColErrorF = true;
                colErrorList.Add(stringBuilder.ToString());
            }
        }

        private string ChineseDir(AngleDir dir)
        {
            string Res = "基准";
            if (dir == AngleDir.TL)
                Res += "左上";
            if (dir == AngleDir.TR)
                Res += "右上";
            if (dir == AngleDir.BR)
                Res += "右下";
            if (dir == AngleDir.BL)
                Res += "左下";
            return Res;
        }
        
        private string ChineseType(AngleType type,string str)
        {
            string Res = str;
            if (type == AngleType.COR)
                Res += "转角";
            if (type == AngleType.BK)
                Res += "避空角";
            if (type == AngleType.XBK)
                Res += "X避空";
            if (type == AngleType.YBK)
                Res += "Y避空";
            return Res;
        }

        //计算两个圆心质检的欧几里得距离
        private double TwoCircleCPDiff(CAD_Point p1,CAD_Point p2)
        {
            double difX = p1.X - p2.X;
            double difY = p1.Y - p2.Y;
            return Math.Sqrt(difX * difX + difY * difY);
        }

        //预估靠近标准计算的Dim
        private void  CloseForeCastDim(ComputedAngleRes coor)
        {
            double Dim = coor.Dim;
            double ResDim = Dim;
            if (Dim <= 4.4)
                ResDim = 4;
            if (4.5 <= Dim && Dim <= 5.5)
                ResDim = 5;
            if (5.65 <= Dim && Dim <= 7)
                ResDim = 6.5;
            if (7.1 <= Dim && Dim <= 9)
                ResDim = 8.5;
            if (9.1 <= Dim && Dim <= 11)
                ResDim = 10.5;
            if (11.1 <= Dim && Dim <= 14)
                ResDim = 13;
            if (14.1 <= Dim && Dim <= 18)
                ResDim = 17.5;
            coor.ProxCheckRadius = ResDim;
        }

        private Boolean PhaseColSecure(ComputedAngleRes coor,StandardHoleData hole,string holeType = null)
        {
            CAD_Point p1 = new CAD_Point();
            p1.X = coor.ArcCPoint.X;
            p1.Y = coor.ArcCPoint.Y;
            CAD_Point p2 = new CAD_Point();
            p2.X = hole.ItemX;
            p2.Y = hole.ItemY;
            double dis = TwoCircleCPDiff(p1, p2);
            double circleSize = hole.ItemDim;
            if(holeType != null)
                circleSize = GetCircleSizeParams(holeType, circleSize);
            double dif = dis - coor.ProxCheckRadius - circleSize / 2;
            return dif <= 1.3;
        }

        //类型转义
        private AngleType GetAngleTypeByStr(string str)
        {
            AngleType Res = AngleType.NONE;
            if (str.Equals("避空"))
                Res = AngleType.BK;
            if (str.Equals("X避空"))
                Res = AngleType.XBK;
            if (str.Equals("Y避空"))
                Res = AngleType.YBK;
            if (str.Equals("R"))
                Res = AngleType.COR;
            return Res;
        }

        public int FindClosestNumber(List<int> numbers, double target)
        {
            int compareNum = (int)target;
            if (numbers == null || numbers.Count == 0)
                throw new ArgumentException("The list cannot be null or empty.");

            int closestNumber = numbers[0];
            int minDifference = Math.Abs(numbers[0] - compareNum);

            foreach (int number in numbers)
            {
                int currentDifference = Math.Abs(number - compareNum);
                if (currentDifference < minDifference)
                {
                    minDifference = currentDifference;
                    closestNumber = number;
                }
            }
            return closestNumber;
        }

        private double GetCircleSizeParams(string ItemStr,double customerValue)
        {
            double Res = 0;
            if (ItemStr.Equals("GP"))
            {
                double gpDim = 0;
                List<StandardGPSP> GPList = ParamsConfigData.StandardGPList;
                List<int> gpDimList = ParamsConfigData.StandardGPList.Select(item => item.Dimension).Distinct().ToList();
                Boolean F = gpDimList.Contains((int)customerValue);
                if (F)
                    gpDim = customerValue;
                else
                    gpDim = FindClosestNumber(gpDimList, customerValue);
                int gpIndex = GPList.FindIndex(item => item.Dimension == gpDim);
                if (gpIndex != -1)
                    Res = GPList[gpIndex].BushSize;
            }
            if (ItemStr.Equals("SP"))
            {
                double spDim = 0;
                List<StandardGPSP> SPList = ParamsConfigData.StandardSPList;
                List<int> spDimList = ParamsConfigData.StandardSPList.Select(item => item.Dimension).Distinct().ToList();
                Boolean F = spDimList.Contains((int)customerValue);
                if (F)
                    spDim = customerValue;
                else
                    spDim = FindClosestNumber(spDimList, customerValue);
                int spIndex = SPList.FindIndex(item => item.Dimension == spDim);
                if (spIndex != -1)
                {
                    Boolean TDF = CADCommonForm.MoldTypeStr.Equals("PP") && BoardCode.Equals("aBoard");
                    if (TDF)
                        Res = SPList[spIndex].BushTD;
                    else
                        Res = SPList[spIndex].BushSize;
                }
            }
            if (ItemStr.Equals("RP"))
            {
                double rpDim = 0;
                List<StandardRP> RPList = ParamsConfigData.StandardRPList;
                List<int> rpDimList = ParamsConfigData.StandardRPList.Select(item => item.Dimension).Distinct().ToList();
                Boolean F = rpDimList.Contains((int)customerValue);
                if (F)
                    rpDim = customerValue;
                else
                    rpDim = FindClosestNumber(rpDimList, customerValue);
                int rpIndex = RPList.FindIndex(item => item.Dimension == rpDim);
                if (rpIndex != -1)
                    Res = RPList[rpIndex].TD;
            }
            return Res;
        }

        //参数调整出图 导柱和拉杆 基准方位的孔 往上方Y 偏2单位
        private void DrawHoleByDyncArc()
        {
            double GPW = (double)this.GPSpacingW.Value / 2;
            double GPL = (double)this.GPSpacingL.Value / 2;
            double SPW = (double)this.SPSpacingW.Value / 2;
            double SPL = (double)this.SPSpacingL.Value / 2;
            double RPW = (double)this.RPSpacingW.Value / 2;
            double RPL = (double)this.RPSpacingL.Value / 2;
            double SPDim = (double)this.SPDim.Value;
            double GPDim = (double)this.GPDim.Value;
            double RPDim = (double)this.RPDim.Value;
            //一般绘制 1 * 4
            if (GPDim > 0 && GPW > 0 && GPL > 0)
            {
                GPList = new List<StandardHoleData>();
                StandardHoleData gpItemTR = new StandardHoleData();
                gpItemTR.ItemX = -GPW;
                gpItemTR.ItemY = -GPL;
                gpItemTR.ItemDim = (int)GPDim;
                GPList.Add(gpItemTR);
                StandardHoleData gpItemTL = new StandardHoleData();
                gpItemTL.ItemX = -GPW;
                gpItemTL.ItemY = GPL;
                gpItemTL.ItemDim = (int)GPDim;
                GPList.Add(gpItemTL);
                StandardHoleData gpItemBR = new StandardHoleData();
                gpItemBR.ItemX = GPW;
                gpItemBR.ItemY = -GPL;
                gpItemBR.ItemDim = (int)GPDim;
                GPList.Add(gpItemBR);
                //BL 默认为基准方位的孔 往上方Y 偏2单位
                StandardHoleData gpItemBL = new StandardHoleData();
                gpItemBL.ItemX = GPW;
                //往上方Y 偏2单位
                gpItemBL.ItemY = GPL - 2;
                gpItemBL.ItemDim = (int)GPDim;
                GPList.Add(gpItemBL);
                //Bush
                double gpSize = GetCircleSizeParams("GP", SPDim);
                DrawHoleGroup(gpSize, GPList);
            }
            if(SPDim > 0 && SPW > 0 && SPL > 0)
            {
                SPList = new List<StandardHoleData>();
                StandardHoleData spItemTR = new StandardHoleData();
                spItemTR.ItemX = -SPW;
                spItemTR.ItemY = -SPL;
                spItemTR.ItemDim = (int)SPDim;
                SPList.Add(spItemTR);
                StandardHoleData spItemTL = new StandardHoleData();
                spItemTL.ItemX = -SPW;
                spItemTL.ItemY = SPL;
                spItemTL.ItemDim = (int)SPDim;
                SPList.Add(spItemTL);
                StandardHoleData spItemBR = new StandardHoleData();
                spItemBR.ItemX = SPW;
                spItemBR.ItemY = -SPL;
                spItemBR.ItemDim = (int)SPDim;
                SPList.Add(spItemBR);
                StandardHoleData spItemBL = new StandardHoleData();
                spItemBL.ItemX = SPW;
                spItemBL.ItemY = SPL - 2;
                spItemBL.ItemDim = (int)SPDim;
                SPList.Add(spItemBL);
                //Bush
                double spSize = GetCircleSizeParams("SP", SPDim);
                DrawHoleGroup(spSize, SPList);
            }
            if(RPDim > 0 && RPW > 0 && RPL > 0)
            {
                RPList = new List<StandardHoleData>();
                StandardHoleData rpItemTR = new StandardHoleData();
                rpItemTR.ItemX = RPW;
                rpItemTR.ItemY = RPL;
                rpItemTR.ItemDim = (int)RPDim;
                StandardHoleData rpItemTL = new StandardHoleData();
                rpItemTL.ItemX = -RPW;
                rpItemTL.ItemY = RPL;
                rpItemTL.ItemDim = (int)RPDim;
                StandardHoleData rpItemBR = new StandardHoleData();
                rpItemBR.ItemX = RPW;
                rpItemBR.ItemY = -RPL;
                rpItemBR.ItemDim = (int)RPDim;
                StandardHoleData rpItemBL = new StandardHoleData();
                rpItemBL.ItemX = -RPW;
                rpItemBL.ItemY = -RPL;
                rpItemBL.ItemDim = (int)RPDim;
                RPList.Add(rpItemTR);
                RPList.Add(rpItemTL);
                RPList.Add(rpItemBR);
                RPList.Add(rpItemBL);
                DrawHoleGroup(RPDim, RPList);
            }
        }

        private void CheckBoardRPHoleCol()
        {
            Boolean ColF = false;
            List<string> tigList = new List<string>();
            StringBuilder stringBuilder = new StringBuilder();
            List<ComputedAngleRes> angList = framePhaseCol.angList;
            List<double> FAYList = angList.Select(item => item.FAPoint.Y).ToList();
            List<double> FAXList = angList.Select(item => item.FAPoint.X).ToList();
            double bx1 = FAXList.Min();
            double bx2 = FAXList.Max();
            double by1 = FAYList.Min();
            double by2 = FAYList.Max();
            if (RPList.Count > 0)
            {
                foreach (var rpItem in RPList)
                {
                    Boolean XF = bx1 <= rpItem.ItemX && rpItem.ItemX <= bx2;
                    if(XF)
                    {
                        Boolean F1 = LyCCol(by1,rpItem);
                        Boolean F2 = LyCCol(by2,rpItem);
                        if (F1 || F2)
                        {
                            ColF = true;
                            if (F2)
                            {
                                string tig = "回针与精框上部相崩! ";
                                tigList.Add(tig);
                            }
                            if (F1)
                            {
                                string tig = "回针与精框下部相崩! ";
                                tigList.Add(tig);
                            }
                        }
                    }  
                }
                if (ColF)
                {
                    tigList = tigList.Distinct().ToList();
                    tigList.ForEach(item => stringBuilder.Append(item));
                    HasColErrorF = true;
                    colErrorList.Add(stringBuilder.ToString());
                }
            }
        }

        private void ReDrawBtn_Click(object sender, EventArgs e)
        {
            ResetColErrorTig();
            ReDraw();
        }

        private void ReDraw()
        {
            ArgDraw();
        }

        private void ShowRuleBtn_Click(object sender, EventArgs e)
        {
            colRuleWindow.Show();
        }

        private void FrameSideMeaBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrameSideMeaAction?.Invoke(operIndex, "TWOT");
        }

        private void FrameSideLabelBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrameSideMeaAction?.Invoke(operIndex, "LABEL");
        }

        private void BoardFrameBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int BIndex = this.BoardFrameBox.SelectedIndex;
            if (AssemblyShareData.operType == CustomOperPatternType.AUTO)
                SetAutoPageFrameLogicAndUIData(BIndex);
            if (AssemblyShareData.operType == CustomOperPatternType.MANUAL)
                SetMPageFrameLogicAndUIData(BIndex);
            ReDraw();
        }

        //保存孔距数据逻辑
        private void UserSaveHoleLogic()
        {
            List<ManualItem> tempList = ShareItemData.MItemList;
            for(int i = 0; i < tempList.Count; i++)
            {
                string matchCode = tempList[i].ItemCode;
                if (matchCode.Equals("GuidePin"))
                {
                    tempList[i].Dim = (double)this.GPDim.Value;
                    tempList[i].ItemHW = (double)this.GPSpacingW.Value;
                    tempList[i].ItemHL = (double)this.GPSpacingL.Value;
                }
                if (matchCode.Equals("SupportPin") || matchCode.Equals("NoNozzleMesonSupportPin"))
                {
                    tempList[i].Dim = (double)this.SPDim.Value; 
                    tempList[i].ItemHW = (double)this.SPSpacingW.Value; 
                    tempList[i].ItemHL = (double)this.SPSpacingL.Value; 
                }
                if (matchCode.Equals("ReturnPin"))
                {
                    tempList[i].Dim = (double)this.RPDim.Value;
                    tempList[i].ItemHW = (double)this.RPSpacingW.Value;
                    tempList[i].ItemHL = (double)this.RPSpacingL.Value;
                }
                if (matchCode.Equals("BottomPanelScrew"))
                {
                    tempList[i].Dim = (double)this.BPDim.Value;
                    tempList[i].ItemHW = (double)this.BPSpacingW.Value;
                    tempList[i].ItemHL = (double)this.BPSpacingL.Value;
                }
                if (matchCode.Equals("PinBoardScrew"))
                {
                    tempList[i].Dim = (double)this.EPFPDim.Value;
                    tempList[i].ItemHW = (double)this.EPFPSpacingW.Value;
                    tempList[i].ItemHL = (double)this.EPFPSpacingL.Value;
                }
                if (matchCode.Equals("TopPanelScrew"))
                {
                    tempList[i].Dim = (double)this.TPDim.Value;
                    tempList[i].ItemHW = (double)this.TPSpacingW.Value;
                    tempList[i].ItemHL = (double)this.TPSpacingL.Value;
                }
                if (matchCode.Equals("SpaceBlockScrew"))
                {
                    tempList[i].Dim = (double)this.fangtieDim.Value;
                    tempList[i].ItemHW = (double)this.fangtieSpacingW.Value;
                    tempList[i].ItemHL = (double)this.fangtieSpacingL.Value;
                }
                if (matchCode.Equals("EGPPin"))
                {
                    tempList[i].Dim = (double)this.EGPDim.Value;
                    tempList[i].ItemHW = (double)this.EGPSpacingW.Value;
                    tempList[i].ItemHL = (double)this.EGPSpacingL.Value;
                }
            }
        }

        //手动模式 保存孔距按钮 点击事件
        private void ComfirmSaveHoleBtn_Click(object sender, EventArgs e)
        {
            UserSaveHoleLogic();
            //通知更改主界面 孔数据UI渲染
            this.MotifyToChangeMainWinHoleUIAction?.Invoke();
            this.Hide();
        }

        //快捷输入
        private void frameLBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                frameWBox.Focus();
                UpDownBase updbText = (UpDownBase)frameWBox;
                frameWBox.Select(0, frameWBox.Text.Length);
            }
        }

        private void frameWBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                frameDBox.Focus();
                UpDownBase updbText = (UpDownBase)frameDBox;
                frameDBox.Select(0, frameDBox.Text.Length);
            }
        }

        private void frameDBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AutoSetDefaultAngleValueLogic();
                cAllValue.Focus();
                UpDownBase updbText = (UpDownBase)cAllValue;
                cAllValue.Select(0, cAllValue.Text.Length);
            }
        }

        private Boolean CheckSetDefaultAngleValue()
        {
            decimal D = frameDBox.Value;
            string cTypeText = this.cAllType.Text;
            Boolean F1 = cTypeText != "";
            if (D > 0 && F1)
                return true;
            return false;
        }

        //根据框深 角类型 设置默认 R值
        public void AutoSetDefaultAngleValueLogic()
        {
            decimal Res = 0.00M;
            Boolean checkF = CheckSetDefaultAngleValue();
            if (checkF)
            {
                decimal D = frameDBox.Value;
                string cTypeText = this.cAllType.Text;
                Boolean D1 = D < 50 ;
                Boolean D2 = 50 <= D && D < 70;
                Boolean D3 = 70 <= D && D <= 100;
                Boolean D4 = 101 <= D && D <= 180;
                Boolean D5 = 181 <= D && D <= 300;
                Boolean R1 = cTypeText.Equals("R");
                Boolean R2 = cTypeText.Equals("避空");
                if(D1)
                {
                    if (R1)
                        Res = 13;
                    if (R2)
                        Res = 8.5M;
                }
                if (D2)
                {
                    if (R1)
                        Res = 16.5M;
                    if (R2)
                        Res = 10.5M;
                }
                if (D3)
                {
                    if (R1)
                        Res = 16.5M;
                    if (R2)
                        Res = 13;
                }
                if (D4)
                {
                    if (R1)
                        Res = 26.5M;
                    if (R2)
                        Res = 17.5M;
                }
                if (D5)
                {
                    if (R1)
                        Res = 33;
                    if (R2)
                        Res = 25;
                }
            }
            if (Res > 0)
            {
                cAllValue.Value = Res;
                TLValue.Value = Res;
                TRValue.Value = Res;
                BLValue.Value = Res;
                BRValue.Value = Res;
            }
        }

        private void cAllValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
               double allRadius = (double)this.cAllValue.Value;
               if (allRadius > 0)
               {
                   this.TLValue.Value = this.cAllValue.Value;
                   this.TRValue.Value = this.cAllValue.Value;
                   this.BLValue.Value = this.cAllValue.Value;
                   this.BRValue.Value = this.cAllValue.Value;

                   operFItem.TL_Radius = allRadius;
                   operFItem.TR_Radius = allRadius;
                   operFItem.BL_Radius = allRadius;
                   operFItem.BR_Radius = allRadius;
               }
            }
        }

    }

    public class AngleArg
    {
        public double Dim { get; set; } = 0;
        public AngleDir angleDir { get; set; } = AngleDir.NONE;
        public AngleType angleType { get; set; } = AngleType.NONE;
    }

    public class ComputedAngleRes
    {
        public double startang { get; set; }
        public double endang { get; set; }
        public double Dim { get; set; }
        public double ProxCheckRadius { get; set; }
        public CAD_Point Point1 { get; set; } = new CAD_Point();
        public CAD_Point Point2 { get; set; } = new CAD_Point();
        // Arc Circle Point
        public CAD_Point ArcCPoint { get; set; } = new CAD_Point();
        //Frame Angle Point
        public CAD_Point FAPoint { get; set; } = new CAD_Point();
        public AngleDir angleDir { get; set; } = AngleDir.NONE;
        public AngleType angleType { get; set; } = AngleType.NONE;
    }


}
